package commRedisService

import (
	"gitee.com/fierce_wolf/go-fox-edge-common/commEntity"
	"gitee.com/fierce_wolf/go-fox-edge-common/commRedis"
	"gitee.com/fierce_wolf/go-fox-edge-common/commUtil/Differ"
	"strconv"
)

func saveEntityMap(entityType string, suffix string, writeMap map[string]interface{}) error {
	mainKey := RedisPrefix + ":" + entityType + ":" + suffix

	if len(writeMap) == 0 {
		return nil
	}

	// 写入全新的数据[string:string]
	_, err := commRedis.Client.HMSet(mainKey, writeMap).Result()
	return err
}

func syncAllEntityMapToRedis(entityType string, suffix string, writeMap map[string]interface{}) error {
	mainKey := RedisPrefix + ":" + entityType + ":" + suffix

	// 写入全新的数据[string:string]
	_, err := commRedis.Client.HMSet(mainKey, writeMap).Result()
	if err != nil {
		return nil
	}

	// 重新读取全部出来：此时可能有多余的旧数据
	readMap, err := commRedis.Client.HGetAll(mainKey).Result()
	if err != nil {
		return nil
	}

	// 比对两边数据
	srcAdd := make(map[string]interface{})
	srcDel := make(map[string]string)
	srcEql := make(map[string]string)
	dstEql := make(map[string]interface{})
	Differ.Map.KStringVStringStruct(readMap, writeMap, srcAdd, srcDel, srcEql, dstEql)

	// 删除多余的数据
	for k := range srcDel {
		commRedis.Client.HDel(mainKey, k)
	}

	return nil
}

func saveSyncTime(entityType string, value interface{}) (string, error) {
	dataKey := RedisPrefix + ":" + entityType + ":sync"
	return commRedis.Client.Set(dataKey, value, -1).Result()
}

func readHkeyDataMap(entityType string, hKeys []string) (map[string]commEntity.IEntity, error) {
	dataKey := RedisPrefix + ":" + entityType + ":data"

	// 转换数据结构
	result := make(map[string]commEntity.IEntity)

	// 检查：key列表是否为空
	if len(hKeys) == 0 {
		return result, nil
	}

	// 获取hash中的全部数据
	m, err := commRedis.Client.HMGet(dataKey, hKeys...).Result()
	if err != nil {
		return nil, err
	}

	for _, v := range m {
		if v == nil {
			continue
		}

		str := v.(string)

		entity, _ := commEntity.BuildEntityByJson(entityType, &str)
		if entity != nil {
			result[entity.MakeServiceKey()] = entity
		}
	}

	return result, nil
}

func readAllDataMap(entityType string) (map[string]commEntity.IEntity, error) {
	dataKey := RedisPrefix + ":" + entityType + ":data"

	// 获取hash中的全部数据
	m, err := commRedis.Client.HGetAll(dataKey).Result()
	if err != nil {
		return nil, err
	}

	// 转换数据结构
	result := make(map[string]commEntity.IEntity)
	for k, v := range m {
		entity, _ := commEntity.BuildEntityByJson(entityType, &v)
		if entity != nil {
			result[k] = entity
		}
	}

	return result, nil
}

func readAllAgileMap(entityType string) (map[string]int64, error) {
	dataKey := RedisPrefix + ":" + entityType + ":agile"

	// 获取hash中的全部数据
	m, err := commRedis.Client.HGetAll(dataKey).Result()
	if err != nil {
		return nil, err
	}

	// 转换数据结构
	result := make(map[string]int64)
	for k, v := range m {
		i64, err := strconv.ParseInt(v, 10, 64)
		if err != nil {
			result[k] = 0
		} else {
			result[k] = i64
		}

	}

	return result, nil
}

func readSyncTime(entityType string) (int64, error) {
	dataKey := RedisPrefix + ":" + entityType + ":sync"

	// 获取hash中的全部数据
	v, err := commRedis.Client.Get(dataKey).Result()
	if err != nil {
		return -1, nil
	}

	i64, err := strconv.ParseInt(v, 10, 64)
	if err != nil {
		return -1, nil
	}

	return i64, nil
}
